// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from '@/store/store'

import '@/styles/index.scss'

Vue.use(ElementUI)
Vue.config.productionTip = false

// 注册一个全局守卫，在路由跳转前，对路有进行判断，防止未登录的用户跳转到其他页面
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('mytoken')
  if (token) {
    // 如果已经登录，就不加干涉
    next()
  } else {
    if (to.path !== '/login') {
      // 如果没有登录，但是要访问登录之后的页面，就跳转到登录页面登录
      next({path: '/login'})
    } else {
      // 如果没有登录，但是访问的是login路径，那么就不加干涉
      next()
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
